/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2013-8-15         xingkong207
 */

package com.skyworth.tvos.checker;

import android.util.Log;

import com.skyworth.framework.skysdk.ipc.SkyContext;
import com.skyworth.tvos.checker.CheckerManager.CheckerTask;
import com.skyworth.tvos.ipcservice.ServiceDispatch;
import com.skyworth.tvos.ipcservice.ServiceDispatch.SkyPackage;
import com.skyworth.tvos.utils.IPCConst;
import com.skyworth.tvos.utils.SkyAppService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wei li
 * @version TODO (write something)
 * @ClassName SkyServiceMoniter
 * @Description TODO (write something)
 * @date 2013-8-15
 */
public class SkyServiceMoniter extends CheckerTask {

    List<SkyPackage> packages = new ArrayList<SkyPackage>();

    /**
     * @param checkInterval
     * @param name
     * @Description TODO(write something)
     */
    public SkyServiceMoniter(int checkInterval, String name, List<SkyPackage> moniterpackages) {
        super(checkInterval, name);
        // TODO Auto-generated constructor stub
        if (moniterpackages != null) {
            packages = moniterpackages;
        }
    }

    /* (non-Javadoc)
     * @see com.skyworth.tvos.systemservice.checker.CheckerManager.CheckerTask#taskRun()
     */
    @Override
    public void taskRun() {
        // TODO Auto-generated method stub
        SkyAppService appService = SkyAppService.getInstance(SkyContext.context);
        if (appService == null) {
            Log.i(IPCConst.TAG, "appService=null");
            return;
        }
        for (int i = 0; i < packages.size(); i++) {
            if (!appService.isAppRunning(packages.get(i).packageName)) {
                Log.d(IPCConst.TAG, packages.get(i).packageName + " will be start.." + packages.get(i).hasStartedTimes + " max=" + packages.get(i).maxCheckTimes);
                if (packages.get(i).hasStartedTimes < packages.get(i).maxCheckTimes) {
                    ServiceDispatch.getInstance().startService(packages.get(i).packageName);
                    packages.get(i).hasStartedTimes++;
                } else {
                    Log.d(IPCConst.TAG, packages.get(i).packageName + " times is enough.");
                }
            } else {
//            	Log.d(IPCConst.TAG, SkyServices[i] + " is running.");
            }
        }
    }
}
